package com.itfitness.mchart.model.dao;

import android.content.Context;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.itfitness.mchart.bean.InviteNotifyBean;
import com.itfitness.mchart.model.db.DatabaseHelper;
import com.j256.ormlite.dao.Dao;

/**
 * @ProjectName: MChart
 * @Package: com.itfitness.mchart.model.dao
 * @ClassName: InviteNotifyDao
 * @Description: java类作用描述 ：邀请通知操作类
 * @Author: 作者名：lml
 * @CreateDate: 2019/4/25 11:22
 * @UpdateUser: 更新者：
 * @UpdateDate: 2019/4/25 11:22
 * @UpdateRemark: 更新说明：
 * @Version: 1.0
 */

public class InviteNotifyDao {
    private Dao<InviteNotifyBean, Integer> inviteNotifyDao;
    private DatabaseHelper helper;

    @SuppressWarnings("unchecked")
    public InviteNotifyDao(Context context)
    {
        try
        {
            helper = DatabaseHelper.getInstance(context);
            inviteNotifyDao = helper.getDao(InviteNotifyBean.class);
        } catch (SQLException e)
        {
            e.printStackTrace();
        }
    }
    // 向表中添加一条数据
    public void insert(InviteNotifyBean data) {
        try {
            inviteNotifyDao.create(data);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    // 删除表中的一条数据
    public void delete(InviteNotifyBean data) {
        try {
            inviteNotifyDao.delete(data);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    // 修改表中的一条数据
    public void update(InviteNotifyBean data) {
        try {
            Map<String,Object> params = new HashMap<>();
            params.put("uname",data.getUsername());
            params.put("invite",data.getInvite());
            params.put("type",data.getType());
            params.put("gid",data.getGroupid());
            List<InviteNotifyBean> users = inviteNotifyDao.queryForFieldValues(params);
            if(users!=null&&users.size()>0){
                data.setId(users.get(0).getId());
                inviteNotifyDao.update(data);
            }else {
                inviteNotifyDao.create(data);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    // 查询表中的所有数据
    public List<InviteNotifyBean> selectAll() {
        List<InviteNotifyBean> users = null;
        try {
            users = inviteNotifyDao.queryForAll();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return users;
    }

    // 查询表中指定用户的所有数据
    public List<InviteNotifyBean> selectAllWithUser(String username) {
        List<InviteNotifyBean> users = null;
        try {
            users = inviteNotifyDao.queryBuilder().orderBy("id",false).where().eq("uname",username).query();
//            users = inviteNotifyDao.queryForEq("uname",username);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return users;
    }
}
